﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Net.Mail;
using System.IO;
using PAMD.Database.EmailService;

namespace PAMD.Database
{
    public class Utils
    {
        //Function to get random number 
        private static readonly Random Random = new Random();
        private static readonly object SyncLock = new object();
        public static int RandomNumber(int min, int max)
        {
            lock (SyncLock)
            { // synchronize 
                return Random.Next(min, max);
            }
        }

        public DateTime GetDate(string date)
        {
            const string format = "dd-MM-yyyy";
            CultureInfo provider = new CultureInfo(CultureInfo.InvariantCulture.LCID);
            DateTime dobNew;
            if (DateTime.TryParseExact(date, format, provider, DateTimeStyles.None, out dobNew))
                return dobNew;

            return DateTime.MinValue;
        }

        #region Converting Time from AM/PM to 24 hrs and viceversa

        /* Method to convert the 24 hrs time HH:MM to 12 hrs format HH:MM AM/PM format
         * Input :-
         *      time the string in 24 hrs format HH:MM
         * returns  string in HH:MM [AM|PM]
         * */
        public static string Convert24To12(string time)
        {
            //            ^(\d|[0]\d|1[0-2]):[0-5]\d [AP]M$
            // ^([0-1][0-9]|[2][0-3]):([0-5][0-9])$
            if (time.IndexOf(":") == -1)
                return time;
            string appendstring = "AM";
            string strHr = time.Substring(0, time.IndexOf(":"));
            string strMin = time.Substring(time.IndexOf(":") + 1);
            int intHr = Convert.ToInt16(strHr);
            if (intHr > 12)
            {
                appendstring = "PM";
                intHr -= 12;
            }
            if (intHr == 12)
            {
                appendstring = "PM";
            }
            else if (intHr == 0)
            {
                intHr = 0;
            }
            return ((intHr < 10) ? "0" : "") + intHr + ":" + strMin + " " + appendstring;

        }


        /* Method to convert the 12 hrs time HH:MM AM/PM to 24 hrs format HH:MM format
         * Input :-
         *      time the string in 12 hrs format HH:MM [AM|PM]
         * returns  string in HH:MM
         * 
         * */
        public static string Convert12To24(string time)
        {
            if (time.IndexOf(":") == -1)
                return time;

            string strHr = time.Substring(0, time.IndexOf(":"));
            string strMin = time.Substring(time.IndexOf(":") + 1, 2);

            int intHr = Convert.ToInt16(strHr);
            if (time.IndexOf("PM") > -1 && intHr != 12)
            {
                intHr += 12;
            }
            else if (time.IndexOf("AM") > -1 && intHr == 12)
            {
                intHr = 0;
            }

            return ((intHr < 10) ? "0" : "") + intHr + ":" + strMin;
        }

        #endregion

        /* Function to convert the HexColour value to System Colour value;
         * Input HexColour : string "#RRGGBB"
         * Return string : colour in string format
         */
        public static string ConvertHexToRgb(string hexColour)
        {
            string hex = hexColour.Replace("#", "");

            return hex;
        }

        public ArrayList HashTableToArrayList(Hashtable objHashTable)
        {
            ArrayList objArrayList = new ArrayList();
            foreach (int key in objHashTable.Keys)
            {
                objArrayList.Add(objHashTable[key]);
            }
            return objArrayList;
        }

        public string GetSixDigitNo(int ackNo)
        {
            int noLenght = ackNo.ToString().Length;
            string finalNumber = ackNo.ToString();
            if (noLenght < 6)
            {
                for (int i = 0; i < (6 - noLenght); i++)
                {
                    finalNumber = "0" + finalNumber;
                }
            }
            return finalNumber;
        }

        public static bool SendMail(string to, string subject, string body)
        {
            if (ConfigurationManager.AppSettings["EmailOn"].ToLower() != "true")
                return false;
            EmailServiceClient client = new EmailServiceClient();
            return client.SendMail(to, subject, body);
        }

        public static string CreateRandomnumber(int length)
        {
            const string pswdchars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
            const string keychars = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
            string allowedChars = "";
            char[] chars = new char[length];
            Random rd = new Random();
            if (length == 6) // FOR SENDING ACTVATION KEY LENGTH = 6
            {
                allowedChars = keychars;
            }
            if (length == 8) // FOR SENDING PASSWORD LENGTH = 8
            {
                allowedChars = pswdchars;
            }
            for (int i = 0; i < length; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }
            return new string(chars);
        }

        public byte[] ImageToBinary(string imagePath)
        {
            FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, (int)fileStream.Length);
            fileStream.Close();
            return buffer;
        }

        public static string GetString(string str)
        {
            if (string.IsNullOrEmpty(str) || str == "0")
            {
                return "Not available";
            }
            return str;
        }

        public static T GetEnumVal<T>(string val)
        {
            return (T)(Enum.Parse(typeof(T),val));
        }
    }
}
